<?php 
class OrderModel extends Model{
	// 定义表名
	public $table = 'myorder';

	// 添加订单
	public function add_orderData(){
		// 获得地址id
		$uaid = Q('post.uaid');
		// 获得地址数据
		$uaData = K('User_add')->where("uaid={$uaid}")->find();
		// 获得订单号
		$onum = Cart::getOrderId();
		// 压入需要数据
		$this->data = array(
			'onum'		=>	Cart::getOrderId(),
			'consignee'	=>	$uaData['uaname'],
			'address'	=>	$uaData['address'],
			'mobile'	=>	$uaData['telphone'],
			'total'		=>	Cart::getTotalPrice(),
			'time'		=>	time(),
			'remark'	=>	Q('post.send-time'),
			'state'		=>	'待发货',
			'user_uid'=>	$_SESSION['uid']
			);
		// 添加数据
		$oid = $this->add();
		// 把oid存到sesion
		session('oid',$oid);
		// 获得购物车数据
		$cartData =  Cart::getAllData();
		// 循环插入订单列表
		$orderList = K('OrderList');
		$goodsList = K('GoodsList');
		foreach ($cartData['goods'] as $k => $v) {
			// 压入需要数据
			$orderList->data = array(
				'buy_num'		=>	$v['num'],
				'subtotal'		=>	$v['total'],
				'goods_gid'	=>	$v['id'],
				'order_oid'	=>	$oid
				);
//				p($cartData['goods']);
			// 插入数据库
			$orderList->add();
			// 获得当前货品id
			$glid = $v['options']['glid'];
			
			// 货品列表减数量
			//将id为4记录的total减1
			//M('news')->dec('total','id=4',1); 
			$goodsList->dec('inventory',"glid={$glid}",$v['num']);
//			p($goodsList);die;
		}
		
		session('cart',null);
		return true;
		
	}
}










 ?>